package com.sx.pipeline.ui.wallet

import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.tabs.TabLayoutMediator
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActWalletBalanceBinding
import com.sx.pipeline.ui.task.adapter.ReleaseTaskPageAdapter


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 钱包余额
 */
@Route(path = RoutePath.Wallet.BALANCE)
class WalletBalanceActivity : CYDataBindingBaseActivity<ActWalletBalanceBinding,WalletVM>(R.layout.act_wallet_balance), View.OnClickListener {
    private lateinit var walletPageAdapter: WalletBalancePageAdapter

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        initPager()
    }

    override fun onResume() {
        super.onResume()
        //获取钱包余额
        mViewModel.updateUserInfo()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.userInfoDataEvent){
            mBinding.tvBalance.text=it?.currencyBalance
        }
    }

    /**
     * 初始化ViewPager
     */
    private fun initPager() {
        if (mBinding.viewPager.adapter == null) {
            walletPageAdapter = WalletBalancePageAdapter(
                mViewModel.getWalletTabsList(),mViewModel.getWalletTabsId(), supportFragmentManager, lifecycle)
        }
        mBinding.viewPager.adapter = walletPageAdapter
        mBinding.viewPager.offscreenPageLimit=mViewModel.getWalletTabsList().size
        mBinding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {

            }
        })

        //绑定tabLayout和viewPager
        TabLayoutMediator(
            mBinding.tabLayout, mBinding.viewPager,
            true, true
        ) { tab, position ->
            tab.text = mViewModel.getWalletTabsList()[position]
        }.attach()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //充值
            R.id.tv_recharge->ARouter.getInstance().build(RoutePath.Wallet.RECHARGE).navigation()
            //提现
            R.id.tv_withdraw->ARouter.getInstance().build(RoutePath.Wallet.WITHDRAW).navigation()
        }
    }

}